/*
 * Java CMPP API Copyright (C) 1998 - 2002 by Xu Youming
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * A copy of the LGPL can be viewed at http://www.gnu.org/copyleft/lesser.html
 * Java CMPP API author: xuym@bc-info.net Java CMPP API Homepage:
 * http://cmppapi.sourceforge.net/ $Id: UCS2Encoding.java,v 1.6 2004/12/03
 * 17:49:47 orank Exp $
 */

package com.bci.cmpp.util;

import java.io.UnsupportedEncodingException;

public class GBKEncoding extends com.bci.cmpp.util.AlphabetEncoding {

	private static final String ENCODING = "GBK";

	private static final int DCS = 15;

	private static GBKEncoding instance = null;

	static {
		try {
			instance = new GBKEncoding();
		} catch (UnsupportedEncodingException x) {
		}
	}

	/**
	 * Construct a new GBK encoding.
	 * 
	 * @throws java.io.UnsupportedEncodingException
	 *             if the GBK charset is not supported by the JVM.
	 */
	public GBKEncoding() throws UnsupportedEncodingException {
		super(DCS);

		// Force an exception if the charset is not supported.
		new String(new byte[0], ENCODING);
	}

	/**
	 * Get an instance of the UCS2Encoding.
	 * 
	 * @throws java.io.UnsupportedEncodingException
	 *             if the ISO-10646-UCS-2 charset is not supported by the JVM.
	 */
	public static GBKEncoding getInstance() throws UnsupportedEncodingException {
		return (instance);
	}

	/**
	 * Decode SMS message text to a Java String. The SMS message is expected to
	 * be in GBK format.
	 */
	public String decodeString(byte[] b) {
		if (b == null)
			return ("");

		try {
			return (new String(b, ENCODING));
		} catch (java.io.UnsupportedEncodingException x) {
			return ("");
		}
	}

	/**
	 * Encode a Java String to bytes using GBK (GB2312).
	 */
	public byte[] encodeString(String s) {
		if (s == null)
			return (new byte[0]);

		try {
			return (s.getBytes(ENCODING));
		} catch (java.io.UnsupportedEncodingException x) {
			return (new byte[0]);
		}
	}
}